Package Parsers

Source Code of Parsers.AvisParser

package Parsers;

import java.util.ArrayList;
import java.util.Collection;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import BusinessObjects.Avis;
import BusinessObjects.Photo;
import Main.MainProgram;

public class AvisParser extends DefaultHandler {
  private MainProgram main;

  private Collection<Avis> listeAvis;
  private Collection<String> avisToDelete;

  private Avis currentAvis;
  private Photo currentPhoto;

  private String currentBalise="";

  public AvisParser(MainProgram m) {
    this.main = m;
    listeAvis = new ArrayList<Avis>();
    avisToDelete = new ArrayList<String>();
  }

  @Override
  public void startElement(String uri, String localName, String qName,
      Attributes attributes) throws SAXException {
    if (localName.equals("avis")) {
      currentAvis = new Avis();

      currentAvis.setActiviteID(attributes.getValue("ActiviteID"));
      currentAvis.setUserID(attributes.getValue("UserID"));
    }
   
    if (localName.equals("photo")){
      currentPhoto = new Photo();
     
      currentPhoto.setPhotoID(Integer.parseInt(attributes.getValue("PhotoID")));
    }
   
    if (localName.equals("ID")){
      avisToDelete.add(attributes.getValue(0));
    }
     
    currentBalise = localName;
  }

  @Override
  public void endElement(String uri, String localName, String qName)
      throws SAXException {

    if (localName.equals("avis"))
      this.listeAvis.add(currentAvis);
   
    if (localName.equals("photo")){
      currentAvis.getPhotos().add(currentPhoto);
    }
  }

  @Override
  public void characters(char[] ch, int start, int length)
      throws SAXException {
    String value = new String(ch, start, length);

    // On supprime les espaces inutiles
    value = value.replaceAll("[\t\n]+", "");
    if (value.equals("") || value.equals(" "))
      return;

   
    if (currentBalise.equals("titre"))
      currentAvis.setTitre(value);
     
    if (currentBalise.equals("commentaires"))
      currentAvis.setCommentaire(value)
     
    if (currentBalise.equals("note"))
      currentAvis.setNote(value);
   
    if(currentBalise.equals("urlPhoto"))
      currentPhoto.setUrlPhoto(value);
   
    if(currentBalise.equals("description"))
      currentPhoto.setDescription(value);
  }

  @Override
  public void endDocument() throws SAXException {
    main.setAvisToCreate(listeAvis);
    main.setAvisToDelete(avisToDelete);

    System.out.println("Fin du fichier : " + MainProgram.avisOperations);
  }
}
TOP

Related Classes of Parsers.AvisParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.